import { login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  avatar: '',
  name: ''
}

const mutations = {
  setToken(state, payload) {
    state.token = payload
    setToken(payload)
  },

  removeToken(state) {
    state.token = ''
    removeToken()
  },
  getInfo(state, payload) {
    state.avatar = payload[0]
    state.name = payload[1]
  }
}

const actions = {
  async login(context, payload) {
    const res = await login(payload)
    console.log(res.data)
    const { avatar, name } = res.data.user
    console.log(avatar, name)
    context.commit('setToken', res.data.token)
    context.commit('getInfo', [avatar, name])
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

